package com.leapp.sk.viewdelegate.classs

import android.content.Context
import android.content.Intent
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import android.widget.*
import cc.vv.lkbasecomponent.util.LKScreenUtil
import cc.vv.lklibrary.log.LogOperate
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.baselibrary.db.dao.AccountDao
import com.example.baselibrary.db.table.AccountTable
import com.example.mvp.bean.ClassMemberInfoEntity
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.*
import com.leapp.sk.R
import com.leapp.sk.app.SKApplication
import com.leapp.sk.ui.activity.classs.ClassDetialActivity
import com.leapp.sk.ui.activity.classs.GroupManagerActivity
import com.leapp.sk.ui.activity.classs.HeadmasterDetialActivity
import com.example.util.finallist.THEACHER_DATA

/**
 * 班级详情视图托管类
 * @author  chaohaipeng  on 2018/7/30.
 */
class ClassDetialDelegate : BaseAppDelegate(){
    /**
     * 老师头像加载操作
     */
    private val mHeadTRequestOptions: RequestOptions = RequestOptions()
    override fun initWidget() {
    }
    override fun initLayoutRes(): Int {
        return R.layout.activity_class_detial
    }

    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        if (!TextUtils.isEmpty(title)){
            val jtb_acd= getView<SKTopBar>(R.id.jtb_acd)
            jtb_acd.setTitle(title)
        }
        setEvent()
    }

    fun setPushbtn(isOpen:Boolean ){
        val iv_acd_message_dustrib= getView<ImageView>(R.id.iv_acd_message_dustrib)
        if(isOpen){
            iv_acd_message_dustrib.isSelected=true
        }else{
            iv_acd_message_dustrib.isSelected=false
        }

    }

    /**
     * 设置B班级Id
     */
    fun setClassID(id:String?){
        val tv_acd_class_id = getView<TextView>(R.id.tv_acd_class_id)
        if(!TextUtils.isEmpty(id)){
            tv_acd_class_id.text = id
        }

    }

    /**
     * 获取提交内容
     */
    fun getNickContent():String{
        val tv_acd_my_nick = getView<TextView>(R.id.tv_acd_my_nick)
        val text = tv_acd_my_nick.text
        return text.toString()
    }
    /**
     * 获取提交内容
     */
    fun setNickContent(nick:String?){
        val tv_acd_my_nick = getView<TextView>(R.id.tv_acd_my_nick)
        if(!TextUtils.isEmpty(nick)){
           tv_acd_my_nick.text = nick
        }
    }

    /**
     * 初始化老师数据
     */
    fun initTechere( context: Context,data: List<ClassMemberInfoEntity>?){
        if (data==null){
            return
        }

        val ll_acd_thecher_head=  getView<LinearLayout>(R.id.ll_acd_thecher_head)
        val hv_acd=  getView<HorizontalScrollView>(R.id.hv_acd)
        for (i in data.indices){
            val get = data.get(i)
            val accountTable = AccountTable()
            accountTable.avatar=get?.avatar
            accountTable.nick=get?.nickName
            accountTable.account=get?.id
            AccountDao.getInstance().createOrUpdate(accountTable)
            val tabView = View.inflate(getActivity<ClassDetialActivity>(), R.layout.adapter_class_detial_techer, null)
            val ll_acdt_rootview = tabView.findViewById<LinearLayout>(R.id.ll_acdt_rootview)
            val ima_acdt_avatar = tabView.findViewById<LKCircleImageView>(R.id.ima_acdt_avatar)
            val tv_acdt_headMaster = tabView.findViewById<TextView>(R.id.tv_acdt_headMaster)

            val tv_acdt_nick =tabView.findViewById<TextView>(R.id.tv_acdt_nick)
            val layoutParams:LinearLayout.LayoutParams = ll_acdt_rootview?.layoutParams as LinearLayout.LayoutParams
            layoutParams.width=LKScreenUtil.getScreenWidth()/5-LKScreenUtil.dp2px(10f)
            ll_acdt_rootview?.layoutParams=layoutParams
            setHead(context, get?.avatar, ima_acdt_avatar)
            tv_acdt_nick.text=get.nickName
            if(get.personnelType==1){
                tv_acdt_headMaster.visibility=View.VISIBLE
            }else{
                tv_acdt_headMaster.visibility=View.GONE
            }

            ll_acd_thecher_head.addView(tabView)
            ll_acdt_rootview.setOnClickListener(View.OnClickListener {
                val intent=Intent(getActivity<AppCompatActivity>(),HeadmasterDetialActivity::class.java)
                intent.putExtra(THEACHER_DATA,get)
                getActivity<AppCompatActivity>().startActivity(intent)
            })

        }
        SKApplication.getInstance()?.mHandler?.postDelayed(object :Runnable{
            override fun run() {
                //滚动到左边
                hv_acd.smoothScrollTo(0,0)
            }

        },10)

    }


    /**
     * 获取提交内容
     */
    fun setclassNameContent(className:String?){
        val tv_acd_my_nick = getView<TextView>(R.id.tv_acd_class_name)
        if(!TextUtils.isEmpty(className)){
            tv_acd_my_nick.text = className
        }
    }

    /**
     * 获取提交内容
     */
    fun getclassNameContent():String{
        val tv_acd_class_name = getView<TextView>(R.id.tv_acd_class_name)
        val text = tv_acd_class_name.text
        return text.toString()
    }

    /**
     * 初始化适配器
     */
    fun initClassMember( context: Context,data: List<ClassMemberInfoEntity>?,total:Int?){
        if (data==null){
            return
        }

        val format = String.format(context.resources.getString(R.string.string_class_member_num),total)
        val tv_acd_class_member_num=  getView<TextView>(R.id.tv_acd_class_member_num)
        val ll_acd_member=  getView<LinearLayout>(R.id.ll_acd_member)
        tv_acd_class_member_num.text=format
        for (i in data.indices){
            val get = data.get(i)
            val accountTable = AccountTable()
            accountTable.avatar=get?.avatar
            accountTable.nick=get?.nickName
            accountTable.account=get?.id
            AccountDao.getInstance().createOrUpdate(accountTable)
            val tabView = View.inflate(getActivity<ClassDetialActivity>(), R.layout.item_class_detial_member, null)
            val ll_icdm_rootview = tabView.findViewById<LinearLayout>(R.id.ll_icdm_rootview)
            val ima_icdm_avatar = tabView.findViewById<LKCircleImageView>(R.id.ima_icdm_avatar)
            val tv_icdm_nick =tabView.findViewById<TextView>(R.id.tv_icdm_nick)
            val layoutParams:LinearLayout.LayoutParams = ll_icdm_rootview?.layoutParams as LinearLayout.LayoutParams
            layoutParams.width=LKScreenUtil.getScreenWidth()/5
            ll_icdm_rootview?.layoutParams=layoutParams
            setHead(context, get?.avatar, ima_icdm_avatar)
            tv_icdm_nick.text=get.nickName
            ll_acd_member.addView(tabView)
            ll_icdm_rootview.setOnClickListener(View.OnClickListener {
                if (get!=null){
                    val intent=Intent(getActivity<AppCompatActivity>(),HeadmasterDetialActivity::class.java)
                    intent.putExtra(THEACHER_DATA,get)
                    getActivity<AppCompatActivity>().startActivity(intent)
                }
            })

        }

    }

    /**
     * 设置头像和昵称
     */
    private fun setHead(context: Context, headUrl: String?, headImage: LKCircleImageView) {

        LogOperate.e("context"+context+"==headUrl"+headUrl+"===headImage"+headImage)
            val mRequestManager = Glide.with(context)
        mHeadTRequestOptions?.diskCacheStrategy(DiskCacheStrategy.NONE)
        mHeadTRequestOptions?.skipMemoryCache(true)
        mHeadTRequestOptions?.error(R.mipmap.test_class_member)
        mHeadTRequestOptions?.transforms(GlideCircleTransform(context))
            mRequestManager?.load(headUrl)
                    ?.thumbnail(0.2f)
                    ?.apply(mHeadTRequestOptions)
                    ?.into(headImage)
    }
    /**
     * 设置设置事件
     */
    fun setEvent(){

        val jtb_acd= getView<SKTopBar>(R.id.jtb_acd)
        jtb_acd.setRightTVContent(getActivity<ClassDetialActivity>().resources.getString(R.string.string_class_manager))
        jtb_acd.setRightTVColor(ContextCompat.getColor(getActivity<ClassDetialActivity>(), R.color.color_474747))
        jtb_acd.getTv_ljtb_right_back()?.visibility= View.VISIBLE
        jtb_acd?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<ClassDetialActivity>().finish()
            }
            override fun rightOnClick() {
                val intent=Intent(getActivity<ClassDetialActivity>(), GroupManagerActivity::class.java)
                getActivity<ClassDetialActivity>().startActivity(intent)

            }

        })
    }

}